package common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 分页工具类
 * @author lby
 *
 */
public class PageInfoUtil
{
	/***
	 * 每页条数
	 */
	private int pageSize = 20 ;
	/***
	 * 当前页
	 */
	private int currentPage;
	/***
	 * 总条数
	 */
	private int totalRecord;
	/***
	 * 总页数
	 */
	private int totalPage;
	/***
	 * 上一页
	 */
	private int prePage ;
	/***
	 * 下一页
	 */
	private int nextPage ;
	/***
	 * 当前页的起始条数
	 */
	private int currentRecord ;
	/*需要set的地方
	 * setPageSize 每页条数
	 * setCurrentPage  当前页
	 * setTotalRecord  总条数
	 * 
	 * */
	public int getPageSize()
	{
		return pageSize;
	}
	public void setPageSize(int pageSize)
	{
		this.pageSize = pageSize;
	}
	
	public int getCurrentPage()
	{
		if(this.currentPage > this.getTotalPage())
		{
			this.currentPage = this.getTotalPage();
		}
		if(this.currentPage < 1)
		{
			this.currentPage = 1 ; 
		}
		return currentPage;
	}
	public void setCurrentPage(int currentPage)
	{
		this.currentPage = currentPage;
	}
	public int getTotalRecord()
	{
		return totalRecord;
	}
	public void setTotalRecord(int totalRecord)
	{
		this.totalRecord = totalRecord;
	}
	public int getTotalPage()
	{
		this.totalPage = this.getTotalRecord()/this.getPageSize();
		if(this.getTotalRecord() %this.getPageSize() !=0 )
		{
			this.totalPage = this.totalPage +1 ;
		}
		return totalPage;
	}
	public int getPrePage()
	{
		this.prePage = this.getCurrentPage()-1;
		if(this.prePage < 1)
		{
			this.prePage = 1 ;
		}
		return prePage;
	}
	public int getNextPage()
	{
		this.nextPage = this.getCurrentPage();
		if(this.nextPage > this.getTotalPage())
		{
			this.nextPage = this.getTotalPage();
		}
		return nextPage;
	}
	public int getCurrentRecord()
	{
		this.currentRecord = (this.getCurrentPage() -1 )*this.pageSize;
		return currentRecord;
	}
	
	public JSONObject toJSON()
	{
		return (JSONObject)JSON.toJSON(this);
	}
	public static void main(String[] args)
	{
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		pageInfoUtil.setTotalRecord(45);
		pageInfoUtil.setCurrentPage(1);
		pageInfoUtil.setPageSize(10);
		
		System.out.println(pageInfoUtil.toJSON());
	}
}
